<template>
	<el-main class="bg edit_wrap comtable_e">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
		<el-row class="row_ce"> 
							<el-col v-if="$check_field('get','expert_user') || $check_field('add','expert_user') || $check_field('set','expert_user')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="专家用户" prop="expert_user">
																					<div v-if="user_group !== '管理员'">
							{{ get_user_session_expert_user(form['expert_user']) }}
							<el-select v-if="(form['medical_recipes_id'] && $check_field('set','expert_user')) || (!form['medical_recipes_id'] && $check_field('add','expert_user'))" id="expert_user" v-model="form['expert_user']" :disabled="disabledObj['expert_user_isDisabled']">
								<el-option v-for="o in list_user_expert_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','expert_user')" id="expert_user" v-model="form['expert_user']" :disabled="true">
								<el-option v-for="o in list_user_expert_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="expert_user" v-model="form['expert_user']" :disabled="disabledObj['expert_user_isDisabled']">
							<el-option v-for="o in list_user_expert_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
																</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','recipe_name') || $check_field('add','recipe_name') || $check_field('set','recipe_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="菜谱名称" prop="recipe_name">
															<el-input id="recipe_name" v-model="form['recipe_name']" placeholder="请输入菜谱名称"
							  v-if="(form['medical_recipes_id'] && $check_field('set','recipe_name')) || (!form['medical_recipes_id'] && $check_field('add','recipe_name'))" :disabled="disabledObj['recipe_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','recipe_name')">{{form['recipe_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','recipe_type') || $check_field('add','recipe_type') || $check_field('set','recipe_type')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="菜谱类型" prop="recipe_type">
											<el-select id="recipe_type" v-model="form['recipe_type']"						v-if="(form['medical_recipes_id'] && $check_field('set','recipe_type')) || (!form['medical_recipes_id'] && $check_field('add','recipe_type'))">
						<el-option v-for="o in list_recipe_type" :key="o['recipe_type']" :label="o['recipe_type']"
							:value="o['recipe_type']">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','recipe_type')">{{form['recipe_type']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','recipe_pictures') || $check_field('add','recipe_pictures') || $check_field('set','recipe_pictures')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="菜谱图片" prop="recipe_pictures">
											<el-upload :disabled="disabledObj['recipe_pictures_isDisabled']" class="avatar-uploader" drag
						accept="image/gif, image/jpeg, image/png, image/jpg" action="" :http-request="upload_recipe_pictures"
						:show-file-list="false" v-if="(form['medical_recipes_id'] && $check_field('set','recipe_pictures')) || (!form['medical_recipes_id'] && $check_field('add','recipe_pictures'))">
						<img id="recipe_pictures" v-if="form['recipe_pictures']" :src="$fullUrl(form['recipe_pictures'])" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
					<el-image v-else-if="$check_field('get','recipe_pictures')" style="width: 100px; height: 100px"
						:src="$fullUrl(form['recipe_pictures'])" :preview-src-list="[$fullUrl(form['recipe_pictures'])]">
						<div slot="error" class="image-slot">
							<img src="../../../public/img/error.png" style="width: 90px; height: 90px" />
						</div>
					</el-image>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','suitable_for_the_target_audience') || $check_field('add','suitable_for_the_target_audience') || $check_field('set','suitable_for_the_target_audience')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="适合人群" prop="suitable_for_the_target_audience">
															<el-input id="suitable_for_the_target_audience" v-model="form['suitable_for_the_target_audience']" placeholder="请输入适合人群"
							  v-if="(form['medical_recipes_id'] && $check_field('set','suitable_for_the_target_audience')) || (!form['medical_recipes_id'] && $check_field('add','suitable_for_the_target_audience'))" :disabled="disabledObj['suitable_for_the_target_audience_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','suitable_for_the_target_audience')">{{form['suitable_for_the_target_audience']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','recipe_calories') || $check_field('add','recipe_calories') || $check_field('set','recipe_calories')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="食谱热量" prop="recipe_calories">
															<el-input id="recipe_calories" v-model="form['recipe_calories']" placeholder="请输入食谱热量"
							  v-if="(form['medical_recipes_id'] && $check_field('set','recipe_calories')) || (!form['medical_recipes_id'] && $check_field('add','recipe_calories'))" :disabled="disabledObj['recipe_calories_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','recipe_calories')">{{form['recipe_calories']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','cooking_steps') || $check_field('add','cooking_steps') || $check_field('set','cooking_steps')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="烹饪步骤" prop="cooking_steps">
															<el-input id="cooking_steps" v-model="form['cooking_steps']" placeholder="请输入烹饪步骤"
							  v-if="(form['medical_recipes_id'] && $check_field('set','cooking_steps')) || (!form['medical_recipes_id'] && $check_field('add','cooking_steps'))" :disabled="disabledObj['cooking_steps_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','cooking_steps')">{{form['cooking_steps']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','cooking_time') || $check_field('add','cooking_time') || $check_field('set','cooking_time')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="烹饪时间" prop="cooking_time">
															<el-input id="cooking_time" v-model="form['cooking_time']" placeholder="请输入烹饪时间"
							  v-if="(form['medical_recipes_id'] && $check_field('set','cooking_time')) || (!form['medical_recipes_id'] && $check_field('add','cooking_time'))" :disabled="disabledObj['cooking_time_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','cooking_time')">{{form['cooking_time']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','recipe_efficacy') || $check_field('add','recipe_efficacy') || $check_field('set','recipe_efficacy')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="食谱功效" prop="recipe_efficacy">
															<el-input id="recipe_efficacy" v-model="form['recipe_efficacy']" placeholder="请输入食谱功效"
							  v-if="(form['medical_recipes_id'] && $check_field('set','recipe_efficacy')) || (!form['medical_recipes_id'] && $check_field('add','recipe_efficacy'))" :disabled="disabledObj['recipe_efficacy_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','recipe_efficacy')">{{form['recipe_efficacy']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','release_time') || $check_field('add','release_time') || $check_field('set','release_time')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="发布时间" prop="release_time">
											<el-date-picker :disabled="disabledObj['release_time_isDisabled']" v-if="(form['medical_recipes_id'] && $check_field('set','release_time')) || (!form['medical_recipes_id'] && $check_field('add','release_time'))" id="release_time"
						v-model="form['release_time']" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
					</el-date-picker>
					<div v-else-if="$check_field('get','release_time')">{{form['release_time']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','required_ingredients') || $check_field('add','required_ingredients') || $check_field('set','required_ingredients')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="所需食材" prop="required_ingredients">
											<el-input type="textarea" id="required_ingredients" v-model="form['required_ingredients']" placeholder="请输入所需食材"
						v-if="(form['medical_recipes_id'] && $check_field('set','required_ingredients')) || (!form['medical_recipes_id'] && $check_field('add','required_ingredients'))" :disabled="disabledObj['required_ingredients_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','required_ingredients')">{{form['required_ingredients']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','recipe_details') || $check_field('add','recipe_details') || $check_field('set','recipe_details')" :xs="24" :sm="24" :lg="24" class="el_form_editor_warp">
							<el-form-item label="食谱详情" prop="recipe_details">
								<quill-editor v-model.number="form['recipe_details']"
						v-if="(form['medical_recipes_id'] && $check_field('set','recipe_details')) || (!form['medical_recipes_id'] && $check_field('add','recipe_details')) ">
					</quill-editor>
					<div v-else-if="$check_field('get','recipe_details')" v-html="form['recipe_details']"></div>
				</el-form-item>
			</el-col>
								<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="审核状态" prop="examine_state">
					<el-select id="examine_state" v-model="form['examine_state']"
						v-if="(form['examine_state'] && $check_examine()) || (!form['examine_state'] && $check_examine())" :disabled="true">
						<el-option key="未审核" label="未审核" value="未审核"></el-option>
						<el-option key="已通过" label="已通过" value="已通过"></el-option>
						<el-option key="未通过" label="未通过" value="未通过"></el-option>
					</el-select>
					<div v-else>{{form["examine_state"]}}</div>
				</el-form-item>
			</el-col>
		
	
	
		
		
	
	
	
		
		
	
		</el-row>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item v-if="$check_action('/medical_recipes/view','set') || $check_action('/medical_recipes/view','add')">
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
				<el-form-item v-else>
					<el-button @click="cancel()">返回</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";
	export default {
		mixins: [mixin],
		data() {
			return {
				field: "medical_recipes_id",
				url_add: "~/api/medical_recipes/add?",
				url_set: "~/api/medical_recipes/set?",
				url_get_obj: "~/api/medical_recipes/get_obj?",
				url_upload: "~/api/medical_recipes/upload?",

				query: {
					"medical_recipes_id": 0,
				},

				form: {
								"expert_user": 0, // 专家用户
										"recipe_name":  '', // 菜谱名称
										"recipe_type":  '', // 菜谱类型
										"recipe_pictures":  '', // 菜谱图片
										"suitable_for_the_target_audience":  '', // 适合人群
										"recipe_calories":  '', // 食谱热量
										"cooking_steps":  '', // 烹饪步骤
										"cooking_time":  '', // 烹饪时间
										"recipe_efficacy":  '', // 食谱功效
										"release_time":  '', // 发布时间
										"required_ingredients":  '', // 所需食材
										"recipe_details":  '', // 食谱详情
									"examine_state": "未审核",
								"medical_recipes_id": 0, // ID
															},
				disabledObj:{
								"expert_user_isDisabled": false,
										"recipe_name_isDisabled": false,
										"recipe_type_isDisabled": false,
										"recipe_pictures_isDisabled": false,
										"suitable_for_the_target_audience_isDisabled": false,
										"recipe_calories_isDisabled": false,
										"cooking_steps_isDisabled": false,
										"cooking_time_isDisabled": false,
										"recipe_efficacy_isDisabled": false,
										"release_time_isDisabled": false,
										"required_ingredients_isDisabled": false,
										"recipe_details_isDisabled": false,
										},

	
					// 用户列表
				list_user_expert_user: [],
						// 用户组
				group_user_expert_user: "",
					
									// 菜谱类型选项列表
				list_recipe_type: [""],
	
			
			
			
			
			
			
			
			
			
		
			}
		},
		methods: {

	
	
				/**
			 * 获取专家用户用户列表
			 */
			async get_list_user_expert_user() {
                var json = await this.$get("~/api/user/get_list?user_group=专家用户");
                if(json.result && json.result.list){
                    this.list_user_expert_user = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					/**
			 * 获取专家用户用户组
			 */
			async get_group_user_expert_user() {
							this.form["expert_user"] = this.user.user_id;
							var json = await this.$get("~/api/user_group/get_obj?name=专家用户");
				if(json.result && json.result.obj){
					this.group_user_expert_user = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_expert_user(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_expert_user.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
									for (let key in _this.form) {
							arrForm.push(key)
						}
												_this.form["expert_user"] = id
									_this.disabledObj['expert_user' + '_isDisabled'] = true
						for (var i=0;i<arr.length;i++){
						  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
							for (var j = 0; j < arrForm.length; j++) {
							  if (arr[i] === arrForm[j]) {
								if (arr[i] !== "expert_user") {
			                      _this.form[arrForm[j]] = res.result.obj[arr[i]]
			                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								  break;
								} else {
								  _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								}
							  }
							}
						  }
						}
					}
				});
			},
					get_user_expert_user(id){
				var obj = this.list_user_expert_user.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
			
				/**
			 * 获取菜谱类型列表
			 */
			
						async get_list_recipe_type() {
				var json = await this.$get("~/api/recipe_classification/get_list?");
				if(json.result && json.result.list){
					this.list_recipe_type = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
								
						/**
			 * 上传菜谱图片
			 * @param {Object} param 图片参数
			 */
			upload_recipe_pictures(param){
									this.uploadFile(param.file, "recipe_pictures");
								},
	
	
			
	
			
	
			
	
			
	
			
	
			
	
			
	
			
	
		
			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
																			
				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
								this.form[key] = form[dbKey]
							}
							if(dbKey === "source_table"){
								this.form['source_table'] = form[dbKey];
							}
							if(dbKey === "source_id"){
								this.form['source_id'] = form[dbKey];
							}
							if(dbKey === "source_user_id"){
								this.form['source_user_id'] = form[dbKey];
							}
						})
					})
				}
										        if (this.form["release_time"] && JSON.stringify(this.form["release_time"]).indexOf("-")===-1){
          this.form["release_time"] = this.$toTime(parseInt(this.form["release_time"]),"yyyy-MM-dd")
        }
							$.db.del("form");

				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){
																																																			if(this.form["release_time"]=="0000-00-00"){
				  this.form["release_time"] = null;
				}
				if(parseInt(this.form["release_time"]) > 9999){
					this.form["release_time"] = this.$toTime(parseInt(this.form["release_time"]),"yyyy-MM-dd")
				}
														

			},

																																																																																																async submit(param, func){
				if (!param) {
					param = this.form;
				}
						
				var pm = this.events("submit_before", Object.assign({}, param)) || param;
				var msg = await this.events("submit_check", pm);
				var ret;
				if (msg) {
					this.$toast(msg, 'danger');
				} else {
																																																																																															ret = this.events("submit_main", pm, func);
				}
				return ret;
			},
			
			/**
			 * 提交前验证事件
			 * @param {Object} 请求参数
			 * @return {String} 验证成功返回null, 失败返回错误提示
			 */
						submit_check(param) {
					
																																																																																																										if (!param.release_time){
					return "发布时间不能为空";
				}
																																						return null;
			},

			is_view(){
				// var bl = this.user_group == "管理员";
				var bl = false;

				if(!bl){
					bl = this.$check_action('/medical_recipes/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/medical_recipes/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/medical_recipes/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/medical_recipes/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/medical_recipes/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
					this.get_list_user_expert_user();
					this.get_group_user_expert_user();
									this.get_list_recipe_type();
																						},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
	
	.img_multiple{
		overflow: hidden;
	}
	.img_multiple .img_block{
		float: left;
		margin-right: 5px;
		margin-bottom: 5px;
		position: relative;
	}
	.img_multiple .img_block img{
		height: 100px;
		width: auto;
	}
	.img_multiple .img_del{
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		background: #0000008a;
		color: #fff;
		line-height: 20px;
		text-align: center;
		border-radius: 100%;
		cursor: pointer;
	}




	
</style>
